package com.company.aicrawlers.controller.api;

import com.company.aicrawlers.common.dto.ApiResponse;
import com.company.aicrawlers.entity.ProductCategoryDict;
import com.company.aicrawlers.service.ProductCategoryDictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 产品类别字典API控制器
 *
 * @author AI Crawlers Team
 * @date 2025-10-10
 */
@Slf4j
@RestController
@RequestMapping("/api/categories")
public class ProductCategoryDictApiController {
    
    @Autowired
    private ProductCategoryDictService categoryDictService;
    
    /**
     * 获取所有类别（树形结构）
     * 
     * 接口：GET /api/categories/tree
     *
     * @return 类别树
     */
    @GetMapping(value = "/tree", name = "getCategoryTree")
    public ApiResponse<List<ProductCategoryDict>> getTree() {
        log.info("获取类别树形结构");
        
        List<ProductCategoryDict> tree = categoryDictService.getTreeList();
        return ApiResponse.success(tree);
    }
    
    /**
     * 获取所有启用的类别（扁平列表）
     * 
     * 接口：GET /api/categories
     *
     * @return 类别列表
     */
    @GetMapping(value = "", name = "getAllCategories")
    public ApiResponse<List<ProductCategoryDict>> getAll() {
        log.info("获取所有类别");
        
        List<ProductCategoryDict> categories = categoryDictService.getAllEnabled();
        return ApiResponse.success(categories);
    }
    
    /**
     * 获取某父类别下的子类别
     * 
     * 接口：GET /api/categories/children
     *
     * @param parentId 父类别ID，默认0（顶级）
     * @return 子类别列表
     */
    @GetMapping(value = "/children", name = "getCategoryChildren")
    public ApiResponse<List<ProductCategoryDict>> getChildren(
            @RequestParam(defaultValue = "0") Long parentId
    ) {
        log.info("获取子类别，父ID: {}", parentId);
        
        List<ProductCategoryDict> children = categoryDictService.getChildren(parentId);
        return ApiResponse.success(children);
    }
    
    /**
     * 获取类别详情
     * 
     * 接口：GET /api/categories/{id}
     *
     * @param id 类别ID
     * @return 类别对象
     */
    @GetMapping(value = "/{id}", name = "getCategoryById")
    public ApiResponse<ProductCategoryDict> getById(@PathVariable("id") Long id) {
        log.info("获取类别详情，ID: {}", id);
        
        ProductCategoryDict category = categoryDictService.getById(id);
        return ApiResponse.success(category);
    }
    
    /**
     * 创建类别
     * 
     * 接口：POST /api/categories
     *
     * @param category 类别对象
     * @return 类别ID
     */
    @PostMapping(value = "", name = "createCategory")
    public ApiResponse<Long> create(@RequestBody ProductCategoryDict category) {
        log.info("创建类别，名称: {}, 父ID: {}", category.getCategoryName(), category.getParentId());
        
        Long categoryId = categoryDictService.create(category);
        return ApiResponse.success(categoryId, "创建成功");
    }
    
    /**
     * 更新类别
     * 
     * 接口：PUT /api/categories/{id}
     *
     * @param id 类别ID
     * @param category 类别对象
     * @return 成功消息
     */
    @PutMapping(value = "/{id}", name = "updateCategory")
    public ApiResponse<Void> update(
            @PathVariable("id") Long id,
            @RequestBody ProductCategoryDict category
    ) {
        log.info("更新类别，ID: {}, 名称: {}", id, category.getCategoryName());
        
        category.setCategoryId(id);
        categoryDictService.update(category);
        return ApiResponse.success("更新成功");
    }
    
    /**
     * 删除类别
     * 
     * 接口：DELETE /api/categories/{id}
     *
     * @param id 类别ID
     * @return 成功消息
     */
    @DeleteMapping(value = "/{id}", name = "deleteCategory")
    public ApiResponse<Void> delete(@PathVariable("id") Long id) {
        log.info("删除类别，ID: {}", id);
        
        categoryDictService.delete(id);
        return ApiResponse.success("删除成功");
    }
    
    /**
     * 批量删除类别
     * 
     * 接口：DELETE /api/categories/batch
     *
     * @param ids 类别ID列表
     * @return 成功消息
     */
    @DeleteMapping(value = "/batch", name = "batchDeleteCategories")
    public ApiResponse<Void> batchDelete(@RequestBody List<Long> ids) {
        log.info("批量删除类别，数量: {}", ids.size());
        
        categoryDictService.batchDelete(ids);
        return ApiResponse.success("批量删除成功");
    }
}

